﻿using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace FFmpegVideoClip.windows
{
    /// <summary>
    /// TimeShow.xaml 的交互逻辑
    /// </summary>
    public partial class TimeWindowShow : Window
    {
        private enum ShowTypeEnum { None,ToFrameTime,ToKeyFrameTime }

        ShowTypeEnum ShowType =  ShowTypeEnum.None;
        public TimeWindowShow()
        {
            InitializeComponent();
            Loaded += TimeWindowShow_Loaded;
        }

        private async void TimeWindowShow_Loaded(object sender, RoutedEventArgs e)
        {
            if (Owner is MainWindow mainWindow)
            {
                switch (ShowType)
                {
                    case ShowTypeEnum.ToFrameTime:
                        await mainWindow.ToFrameTimeAsync();
                        break;
                    case ShowTypeEnum.ToKeyFrameTime:
                        await mainWindow.ToKeyFrameTimeAsync();
                        break;
                    default:
                        break;
                }
            }
            this.Close();
        }

        public static void ShowTimeDialog(Window? Owner = null, string? Text = null, int ShowTimeMilliseconds = 1000)
        {
            TimeWindowShow ts = new TimeWindowShow();
            ts.Owner = Owner ?? Application.Current.MainWindow;
            if (Text != null)
                ts.ShowText.Text = Text;
            Task.Run(() =>
            {
                Thread.Sleep(ShowTimeMilliseconds);
                ts.Dispatcher.BeginInvoke(ts.Close);
            });
            ts.ShowDialog();
        }

        public static void ToFrameTimeDialog(Window? Owner)
        {
            TimeWindowShow ts = new TimeWindowShow();
            ts.Owner = Owner ?? Application.Current.MainWindow;
            ts.ShowText.Text = "搜索帧时间中....";
            ts.ShowType = ShowTypeEnum.ToFrameTime;
            ts.ShowDialog();
        }

        public static void ToKeyFrameTimeDialog(Window? Owner)
        {
            TimeWindowShow ts = new TimeWindowShow();
            ts.Owner = Owner ?? Application.Current.MainWindow;
            ts.ShowText.Text = "搜索关键帧时间中....";
            ts.ShowType = ShowTypeEnum.ToKeyFrameTime;
            ts.ShowDialog();
        }
    }
}
